﻿namespace GrupFirmalarTahsilatGirisi
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.IO;
    using System.Windows.Forms;

    public static class db_man
    {
        private static SqlConnection _Connection = null;
        public static SqlDataAdapter _SqlDataAdapter = null;
        public static string Seri = "";
        public static string ServerAdi = "";
        public static string ServerData = "";
        public static string ServerKullaniciAdi = "";
        public static string ServerParola = "";

        public static string Constr()
        {
            VerileriOku();
            return ("Data Source=" + ServerAdi + ";User Id=" + ServerKullaniciAdi + ";Password=" + ServerParola + ";Initial Catalog=" + ServerData + ";Application Name=BARKOD;Pooling=true;Timeout=456755");
        }

        public static bool hasActiveConnection()
        {
            _Connection = new SqlConnection();
            if (_Connection.State != ConnectionState.Open)
            {
                sqlConnect();
            }
            return true;
        }

        public static void sqlConnect()
        {
            if (_Connection == null)
            {
                _Connection = new SqlConnection();
            }
            _Connection.ConnectionString = Constr();
            _Connection.Open();
        }

        public static void sqlDisConnect()
        {
            if (_Connection != null)
            {
                _Connection.Close();
            }
        }

        public static DataTable sqlGetDataTable(SqlCommand mCommand)
        {
            DataTable dataTable = new DataTable("Table");
            if (hasActiveConnection())
            {
                mCommand.Connection = _Connection;
                try
                {
                    new SqlDataAdapter(mCommand).Fill(dataTable);
                }
                catch (SqlException)
                {
                }
            }
            sqlDisConnect();
            return dataTable;
        }

        public static DataTable sqlGetDataTableEdit(SqlCommand mCommand)
        {
            DataTable dataTable = new DataTable("Table");
            if (hasActiveConnection())
            {
                mCommand.Connection = _Connection;
                SqlDataAdapter adapter = new SqlDataAdapter(mCommand);
                adapter.Fill(dataTable);
                _SqlDataAdapter = adapter;
            }
            return dataTable;
        }

        public static int sqlGetScalarValue(SqlCommand mCommand)
        {
            int num = 0;
            if (hasActiveConnection())
            {
                mCommand.Connection = _Connection;
                try
                {
                    num = Convert.ToInt32(mCommand.ExecuteScalar().ToString());
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                catch (Exception exception2)
                {
                    throw new Exception(exception2.Message);
                }
            }
            sqlDisConnect();
            return num;
        }

        public static string sqlGetScalarValue(SqlCommand mCommand, string ds)
        {
            string str = "";
            if (hasActiveConnection())
            {
                mCommand.Connection = _Connection;
                try
                {
                    str = Convert.ToString(mCommand.ExecuteScalar().ToString());
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                catch (Exception exception2)
                {
                    throw new Exception(exception2.Message);
                }
            }
            return str;
        }

        public static void sqlRunCommand(SqlCommand mCommand)
        {
            if (hasActiveConnection())
            {
                mCommand.Connection = _Connection;
                try
                {
                    mCommand.ExecuteNonQuery();
                    sqlDisConnect();
                }
                catch (SqlException exception)
                {
                    throw new Exception(exception.Message);
                }
                catch (Exception exception2)
                {
                    throw new Exception(exception2.Message);
                }
            }
        }

        public static void Update(DataTable Table)
        {
            SqlCommandBuilder builder = new SqlCommandBuilder(_SqlDataAdapter);
            _SqlDataAdapter.InsertCommand = builder.GetInsertCommand();
            _SqlDataAdapter.UpdateCommand = builder.GetUpdateCommand();
            _SqlDataAdapter.DeleteCommand = builder.GetDeleteCommand();
            try
            {
                _SqlDataAdapter.Update(Table);
            }
            catch
            {
            }
            sqlDisConnect();
        }

        private static void VerileriOku()
        {
            if (File.Exists(Application.StartupPath + @"\Setting.cfg"))
            {
                StreamReader reader = new StreamReader(Application.StartupPath + @"\Setting.cfg");
                ServerAdi = reader.ReadLine();
                ServerKullaniciAdi = reader.ReadLine();
                ServerParola = reader.ReadLine();
                ServerData = reader.ReadLine();
                reader.ReadLine();
                reader.ReadLine();
                Seri = reader.ReadLine();
                reader.Close();
            }
        }
    }
}

